package com.notary.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.notary.entity.Notary;
import com.notary.entity.po.NotaryDTO;
import com.notary.entity.vo.JsonResult;
import com.notary.seal.PersonalRegisterRequest;
import com.notary.service.NotaryService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;

/**
 * <p>
 * 公证员基本信息 前端控制器
 * </p>
 * @author xujun
 * @since 2020-07-28
 */


@Api(tags = "公证员基本信息控制器")
@RequestMapping("/notary")
@RestController
@Slf4j
public class NotaryController{


    NotaryService notaryService;

    @Autowired
    public  NotaryController(NotaryService notaryService){
        this.notaryService = notaryService;
    }


    /**
     * <p>
     * 根据条件查询全部记录（并翻页）
     * </p>
     */

    @ApiOperation(value = "列表查询")
    @PostMapping("/list")
    public JsonResult<IPage<Notary>> list(@RequestBody Notary vo, int pageNum, int pageSize) {
        Page<Notary> page = new Page<Notary>(pageNum,pageSize);
        QueryWrapper<Notary> queryWrapper= new QueryWrapper<Notary>();
        //queryWrapper.setEntity(vo);
        if(!StringUtils.isEmpty(vo.getName())){
            queryWrapper.like("name",vo.getName());
        }
        IPage<Notary> iPage = notaryService.page(page,queryWrapper);
        return JsonResult.success(iPage);
    }


    /**
     *<P>
     * 根据ID查找返回单个对象
     *</P>
     */
    @ApiOperation(value = "根据id查询")
    @GetMapping("/find/{id}")
    public JsonResult<Notary> getById(@PathVariable("id") Integer id) {
        return JsonResult.success(notaryService.getById(id));
    }

    /**
     *<P>
     * 根据ID查找返回单个对象
     *</P>
     */
    @ApiOperation(value = "根据id查询")
    @GetMapping("/getByUserId/{userId}")
    public JsonResult<Notary> getByUserId(@PathVariable("userId") Integer userId) {
        return JsonResult.success(notaryService.getByUserId(userId));
    }

    /**
     *<p>
     * 保存对象-如果对象存在则修改原数据，否则插入一条新数据
     *</p>
     */
    @ApiOperation(value = "保存对象")
    @PostMapping("/save")
    public JsonResult saveOrUpdate(@RequestBody NotaryDTO dto) {
        return notaryService.saveNotary(dto);
    }


    /**
     *<p>
     * 根据 id删除记录-逻辑删除
     *</p>
     */
    @ApiOperation(value = "删除")
    @GetMapping("/remove/{id}")
    public JsonResult removeById(@PathVariable("id") Integer id) {
        return JsonResult.success(notaryService.removeById(id));
    }
    /**
     *<p>
     * 我的工作台
     *</p>
     */
    @ApiOperation(value = "我的工作台")
    @PostMapping ("/workbench")
    public JsonResult workbench(HttpServletRequest httpServletRequest) {
        return notaryService.workbench(httpServletRequest);
    }

    /**
     *<p>
     * 保存出证密码
     *</p>
     */
    @ApiOperation(value = " 保存出证密码")
    @PostMapping("/saveIssuePassword")
    public JsonResult updateIssuePassword(@RequestBody Notary notary, HttpServletRequest httpServletRequest) {
        return notaryService.saveIssuePassword(notary,httpServletRequest);
    }
    /**
     *<p>
     * 校验出证密码
     *</p>
     */
    @ApiOperation(value = " 保存出证密码")
    @PostMapping("/checkIssuePassword")
    public JsonResult checkIssuePassword(@RequestBody Notary notary, HttpServletRequest httpServletRequest) {
        return notaryService.checkIssuePassword(notary,httpServletRequest);
    }

    /**
     *<p>
     * 申请用户签名
     *</p>
     */
    @ApiOperation(value = " 申请用户签名")
    @PostMapping("/applyPersonalRegister")
    public JsonResult applyPersonalRegister( PersonalRegisterRequest vo,MultipartFile file) {
        return  notaryService.applyPersonalRegister(vo,file);
    }
    /**
     *<p>
     * 获取个人签名图片
     *</p>
     */
    @ApiOperation(value = "获取个人签名图片")
    @PostMapping("/getPersonalRegisterImg")
    public JsonResult getPersonalRegisterImg( HttpServletRequest httpServletRequest) {
        return  notaryService.getPersonalRegisterImg(httpServletRequest);
    }



}

